//============================================================================
// Name        : cpp_ftp.cpp
// Author      : darkflash
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
#include <malloc.h>
#include <string.h>
using namespace std;

#define BUFFER_SIZE  (256 * 1024)  /* 256 KB */

static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream){
	    char *data = (char *)stream;

	    memcpy(data, ptr, size*nmemb);
	    return size * nmemb;
}

int main() {
	CURL *curl;
	CURLcode res;
	char *data = NULL;

	curl_global_init(CURL_GLOBAL_DEFAULT);
	curl = curl_easy_init();
	if(curl){
		data = (char*)malloc(BUFFER_SIZE);

		curl_easy_setopt(curl, CURLOPT_URL, "ftp://localhost");
		curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
		curl_easy_setopt(curl, CURLOPT_USERPWD, "khs:khs1234");
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);

		res = curl_easy_perform(curl);

		/* always cleanup */
		curl_easy_cleanup(curl);

		if(CURLE_OK != res) {
			/* we failed */
			fprintf(stderr, "curl told us %d\n", res);
		}
	}

	curl_global_cleanup();
	cout << data << endl;
	return 0;
}
